using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using System.Linq;

namespace DIALOGUE.LogicalLines
{
    public class LogicalLineManager
    {
        private DialogueSystem dialogueSystem => DialogueSystem.instance;
        private List<ILogicalLine> logicalLines = new List<ILogicalLine>();

        public LogicalLineManager() => LoadLogicalLines();
        private Coroutine currentLogic = null;

        private void LoadLogicalLines()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            Type[] lineTypes = assembly.GetTypes()
                                            .Where(t => typeof(ILogicalLine).IsAssignableFrom(t) && !t.IsInterface)
                                            .ToArray();

            foreach(Type type in lineTypes)
            {
                ILogicalLine line = (ILogicalLine)Activator.CreateInstance(type);
                logicalLines.Add(line);
            }
        }

        public bool TryGetLogic(DIALOGUE_LINE line, out Coroutine logic)
        {
            //line.rawData;
            foreach (var logicalLine in logicalLines)
            {
                if (logicalLine.Matches(line))
                {
                    logic = dialogueSystem.StartCoroutine(logicalLine.Execute(line));
                    currentLogic = logic;
                    return true;
                }
            }

            logic = null;
            currentLogic = logic;
            return false;
        }

        public void StopCurrentLogic(Coroutine logic)
        {


        }


    }
}